﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuotationService.RealtimeQuotation
{
    internal class ETFEntityConverter :
         IEntityConverter<Quotation.ETF.BasicInfo, ETFBasicInfo>
    {

        public Quotation.ETF.BasicInfo Convert(ETFBasicInfo basicInfo)
        {
            var rtn = new Quotation.ETF.BasicInfo()
            {
                FundId = new Quotation.SymbolCode(basicInfo.Code),
                StkId = new Quotation.SymbolCode(basicInfo.StkId),
                Name = basicInfo.Name,
                ComponentStockQty = basicInfo.ComponentStockQty,
                CreationFlag = basicInfo.CreationFlag,
                CreationRedemptionUnit = basicInfo.CreationRedemptionUnit,
                EstimateSettlementCashAmt = basicInfo.EstimateSettlementCashAmt,
                SettlementCashAmt = basicInfo.SettlementCashAmt,
                ExternalTotalAmount = basicInfo.ExternalTotalAmount,
                FundMarket = basicInfo.FundMarket,
                LocalExchCashId = basicInfo.LocalExchCashId,
                LocalExchStkCount = basicInfo.LocalExchStkCount,
                MaxCashRatio = basicInfo.MaxCashRatio,
                NAV = basicInfo.NAV,
                NAVperCU = basicInfo.NAVperCU,
                OtherExchCashId = basicInfo.OtherExchCashId,
                OtherExchStkCount = basicInfo.OtherExchStkCount,
                PreTradeDate = Quotation.Date.FromYMMDD(basicInfo.PreTradeDate),
                RedemptionFlag = basicInfo.RedemptionFlag,
                TotalAmt = basicInfo.TotalAmt,
                TradeDate = Quotation.Date.FromYMMDD(basicInfo.TradeDate),
                ETFType = basicInfo.ETFType,
                Version = basicInfo.Version,
                UpdateTime = basicInfo.UpdateTime,
                Components = new List<Quotation.ETF.ETFComponent>()
            };
            if (basicInfo.Components != null)
            {
                foreach (var c in basicInfo.Components)
                {
                    rtn.Components.Add(Convert(c));
                }
            }


            return rtn;
        }

        private Quotation.ETF.ETFComponent Convert(ETFComponent etfComponent)
        {
            return new Quotation.ETF.ETFComponent()
            {
                ApplyCashReplaceAmt = etfComponent.ApplyCashReplaceAmt,
                StkId = new Quotation.SymbolCode(etfComponent.Code),
                ExecuteQty = etfComponent.ExecuteQty,
                Name = etfComponent.Name,
                PremiumRate = etfComponent.PremiumRate,
                RedemptionCashReplaceAmt = etfComponent.RedemptionCashReplaceAmt,
                ReplaceFlag = etfComponent.ReplaceFlag,
                TradeFlag = etfComponent.TradeFlag
            };
        }

        public ETFBasicInfo Convert(Quotation.ETF.BasicInfo basicInfo)
        {
            var rtn = new ETFBasicInfo()
            {
                Code = basicInfo.FundId.LongCode,
                StkId = basicInfo.StkId.LongCode,
                Name = basicInfo.Name,
                ComponentStockQty = basicInfo.ComponentStockQty,
                CreationFlag = basicInfo.CreationFlag,
                CreationRedemptionUnit = basicInfo.CreationRedemptionUnit,
                EstimateSettlementCashAmt = basicInfo.EstimateSettlementCashAmt,
                SettlementCashAmt = basicInfo.SettlementCashAmt,
                ExternalTotalAmount = basicInfo.ExternalTotalAmount,
                FundMarket = basicInfo.FundMarket,
                LocalExchCashId = basicInfo.LocalExchCashId,
                LocalExchStkCount = basicInfo.LocalExchStkCount,
                MaxCashRatio = basicInfo.MaxCashRatio,
                NAV = basicInfo.NAV,
                NAVperCU = basicInfo.NAVperCU,
                OtherExchCashId = basicInfo.OtherExchCashId,
                OtherExchStkCount = basicInfo.OtherExchStkCount,
                PreTradeDate = basicInfo.PreTradeDate.ToYMMDD(),
                RedemptionFlag = basicInfo.RedemptionFlag,
                TotalAmt = basicInfo.TotalAmt,
                TradeDate = basicInfo.TradeDate.ToYMMDD(),
                ETFType = basicInfo.ETFType,
                Version = basicInfo.Version,
                UpdateTime = basicInfo.UpdateTime

            };
            if (basicInfo.Components != null)
            {
                List<ETFComponent> components = new List<ETFComponent>();
                foreach (var c in basicInfo.Components)
                {
                    components.Add(Convert(c));
                }
                rtn.Components = components.ToArray();
            }
            return rtn;
        }

        private ETFComponent Convert(Quotation.ETF.ETFComponent etfComponent)
        {
            return new ETFComponent()
            {
                ApplyCashReplaceAmt = etfComponent.ApplyCashReplaceAmt,
                Code = etfComponent.StkId.LongCode,
                ExchID = etfComponent.StkId.ExchId,
                ExecuteQty = etfComponent.ExecuteQty,
                Name = etfComponent.Name,
                PremiumRate = etfComponent.PremiumRate,
                RedemptionCashReplaceAmt = etfComponent.RedemptionCashReplaceAmt,
                ReplaceFlag = etfComponent.ReplaceFlag,
                TradeFlag = etfComponent.TradeFlag

            };
        }
    }
}
